#include "ColumnSet.h"
#include "SQLFlag.h"
#include "DataType.h"
#include "ColumnSet.h"
#include "Column.h"
#include "Value.h"
#include "Record.h"
#include "SQLiteWrapper.h"
#include <vector>
#include <string>
#include <iostream>
#include <assert.h>

using std::string;
using std::cout;
using std::endl;
using std::vector;
using namespace duomai::im;

void ShowLog(bool rc, string tag) {
    if (rc) {
        cout << tag << " done" << endl;
    } else {
        cout << tag << " failed" << endl;
    }
}

int main(int argc, char *argv[])
{
    vector<Column> columns = {
        Column("valueTextTest", SQLITEWRAPPER_TEXT, FLAG_NOT_NULL),
		Column("valueInt", SQLITEWRAPPER_INTEGER),
		Column("valueDbl", SQLITEWRAPPER_FLOAT),
		Column("valueTxt", SQLITEWRAPPER_TEXT, FLAG_UNIQUE),
		Column("valueBol", SQLITEWRAPPER_INTEGER, FLAG_NOT_NULL),
		Column("valueTme", SQLITEWRAPPER_INTEGER),
    };

    Value valueText("valueText", SQLITEWRAPPER_TEXT);
    Value valueInt("123", SQLITEWRAPPER_INTEGER);

    cout << "====== Value Test ======" << endl;
    cout << valueText.toString() << endl;
    cout << valueText.toSQL(SQLITEWRAPPER_TEXT) << endl;

    cout << valueInt.toString() << endl;
    cout << valueInt.toSQL(SQLITEWRAPPER_INTEGER) << endl;

    cout << "====== ColumnSet Test ======" << endl;
    ColumnSet colSet = ColumnSet(columns);
    cout << "ColumnSet toString" << endl;
    cout << colSet.toString() << endl;
    cout << "ColumnSet toSQLCreate" << endl;
    cout << colSet.toSQLCreate() << endl;
    cout << "ColumnSet count is :" << colSet.getCount() << endl;

    for (int i = 0; i < colSet.getCount(); ++i) {
        Column* pCol = colSet.getByIndex(i);
        cout << pCol->getIndex() << endl;
        cout << pCol->getColumnName() << endl;
    }

    cout << "====== RecordSet Test ======" << endl;
    Record record(&colSet);
    record.put("valueTextTest", "Test");
    record.put("valueTxt", "HelloWorld");
    record.put("valueInt", (int64_t)521);
    record.put("valueDbl", 3.1415926);
    record.put("valueTxt", "Txt");
    record.put("value", "adadsf");
    record.put("valueBol", "fadfs");
    record.put("valueTme", (int64_t)5415645);
    cout << record.toString() << endl;
    cout << record.toSQL() << endl;
    cout << record.toSQLInsert("Test") << endl;
    cout << record.toSQLUpdate("Test", "") << endl;

    cout << "====== SQLiteWrapper Test ======" << endl;
    SQLiteWrapper db_ ;
    db_.Open("test.db");
    bool rc = db_.CreateTable("COMPANY", &colSet);
    ShowLog(rc, "open");
    rc = db_.insert("COMPANY", &record);
    ShowLog(rc, "insert");
    record.put("valueTme", (int64_t)180);
    rc = db_.update("COMPANY", &record, "WHERE valueTme=5415645");
    ShowLog(rc, "update");
    rc = db_.Close();
    ShowLog(rc, "Close");
    return 0;
}

